package http;

import android.content.Context;
import android.util.Log;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.client.multipart.HttpMultipartMode;
import com.lidroid.xutils.http.client.multipart.MultipartEntity;
import com.lidroid.xutils.http.client.multipart.content.FileBody;
import com.lidroid.xutils.http.client.multipart.content.StringBody;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

import util.BaseUtil;

/**
 * Created by Administrator on 2015/12/23.
 */
public class HttpHelper {
    private HttpCallBack callBack;
    private static String TAG = HttpHelper.class.getSimpleName();
    /**
     * httpPost
     */
    public void httpPost(Context context,String url, Map<String, Object> map, HttpCallBack mCallback) {
        postImage(context,url,map,mCallback,null);
    }
    /**
     * ����
     */
    private void httpRequest(HttpUriRequest httpUriRequest) {
        String result = null;
        HttpResponse response = null;
        HttpClient httpClient = initHttp();
        try {
            response = httpClient.execute(httpUriRequest);
        } catch (ClientProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        if (response == null){
            Log.d(TAG, "httpRequest null");
            callBack.onFailure(400,"与服务器失去连接");
            return;
        }
        else
            Log.d(TAG, "StatusLine:" + response.getStatusLine());

        if (response != null && response.getStatusLine().getStatusCode() == 200) {
            try {
                HttpEntity httpentity = response.getEntity();
                result = EntityUtils.toString(httpentity);
                Log.e("data",result);
                callBack.onSuccess(result);
            } catch (Exception e) {
                callBack.onFailure(response.getStatusLine().getStatusCode(),"与服务器失去连接");
                e.printStackTrace();
            }
        }else{
            callBack.onFailure(response.getStatusLine().getStatusCode(),"与服务器失去连接");
        }

    }
    public void postImage(Context context, String url, Map<String, Object> map, HttpCallBack mCallback,
                          List<String> imgs) {
        this.callBack = mCallback;
        HttpPost httpPost = new HttpPost(url);
        MultipartEntity mEntity = new MultipartEntity(
                HttpMultipartMode.STRICT,null, Charset.forName("UTF-8"));
        try {
            if (map != null) {
                for (String name : map.keySet()) {
                    System.out.println(name + ":"
                            + String.valueOf(map.get(name)));
                    mEntity.addPart(name,
                            new StringBody(String.valueOf(map.get(name)), Charset.forName("UTF-8")));
                }
            }
            if (imgs != null) {
                for (String fiString : imgs) {
                    File mFile = new File(fiString);
                    if(mFile.exists()&&mFile.isFile()){
                    mEntity.addPart("path",
                            new FileBody(mFile));
                    }
                }
            }
            httpPost.addHeader("token", BaseUtil.getToken(context));
            httpPost.setEntity(mEntity);
             httpRequest(httpPost);
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();

        }

    }
    public HttpClient initHttp() {
        return new HttpUtils().getHttpClient();
    }
}
